package com.ll.DataRecorder;

import org.sqlite.jdbc4.JDBC4Connection;

import javax.naming.NoPermissionException;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import java.util.Properties;

/**
 * 用于存储数据到sqlite的工具
 *
 * @author 陆
 * @address <a href="https://t.me/blanksig"/>click
 */
public class DBRecorder extends BaseRecorder {
    protected Connection conn;
    private Statement stmt;
    protected String cur;
    protected Map<String, Object> data;

    public DBRecorder() {
        this("");
    }

    public DBRecorder(Integer cacheSize) {
        this("", cacheSize);
    }

    public DBRecorder(Path path) {
        this(path, null);
    }

    public DBRecorder(String path) {
        this(path, null);
    }

    public DBRecorder(Path path, Integer cacheSize) {
        this(path, cacheSize, null);
    }

    public DBRecorder(String path, Integer cacheSize) {
        this(path, cacheSize, null);
    }

    public DBRecorder(Path path, Integer cacheSize, String table) {
        this(path.toAbsolutePath().toString(), cacheSize, table);
    }

    public DBRecorder(String path, Integer cacheSize, String table) {
        super(path, cacheSize);
        this.table = table;
    }

    @Override
    protected void init() {
        super.init();
        this.type = "db";
        if (this.path != null) {
            this.set().path(path, table);
        }
    }

    /**
     * @return 返回用于设置属性的对象
     */
    public DBSetter set() {
        if (this.setter == null) this.setter = new DBSetter(this);
        return (DBSetter) this.setter;
    }

    @Override
    public void addData(Object data) {
        addData(data, null);
    }

    public void addData(Object data, String table) {
//        while (pauseAdd) {
//            try {
//                TimeUnit.MILLISECONDS.sleep(100);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//        }
//        table = (table != null) ? table : this.table;
//        if (table == null) {
//            throw new RuntimeException("未指定数据库表名。");
//        }
//        List<Object> dataList = new ArrayList<>();
//        if (data instanceof Map || (data instanceof List && !((List<?>) data).isEmpty()
//                                    && !(((List<?>) data).get(0) instanceof List)
//                                    && !(((List<?>) data).get(0) instanceof Map))) {
//            dataList.add(Tools.dataToListOrDict(this, data));
//        } else if (data instanceof List) {
//            for (Object d : (List<?>) data) {
//                dataList.add(Tools.dataToListOrDict(this, d));
//            }
//        } else {
//            dataList.add(new ArrayList<>());
//        }
//        this.data.computeIfAbsent(table, k -> new ArrayList<>()).addAll(dataList);
//        this.dataCount += dataList.size();
//        if (cacheSize > 0 && cacheSize <= dataCount) {
//            record();
//        }
    }

    /**
     * 连接数据库
     */
    private void connect() throws SQLException {
        File file = Paths.get(this.path).getParent().toFile();
        if (!file.exists()) file.mkdirs();
        this.conn = new JDBC4Connection(file.toString(), this.table, new Properties());
        this.stmt = this.conn.createStatement();
    }

    /**
     * 关闭数据库
     */
    private void close() throws SQLException {
        if (this.conn != null) {
            this.stmt.close();
            this.conn.close();
        }
    }

    @Override
    protected void _record() throws NoPermissionException {

    }
}
